package com.wondersgroup.fjzlpt.yyzyzx.dao.impl.yyzycx;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.page.PageQuerySQLBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.boot.repository.sql.jdbcTemplate.rs.ResultHandlerConstant;
import com.wondersgroup.fjzlpt.yyzyzx.dao.inter.yyzycx.HyxxDAO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 号源信息数据访问实现层
 * <p>
 * <p>
 * Created by sanma on 2017/8/7.
 */
@Repository
public class HyxxDAOImpl extends BaseJTDAOImpl implements HyxxDAO {

    @Override
    public JSONObject pageQueryHyxxAll(Map map) {

        JSONObject sqlObj = this.getQueryPageSql(map);
        List list = (List)sqlObj.get("sqlParams");
        PageQuerySQLBean sb = checkAndCreatePageQuerySQLBean(map, sqlObj.getString("sql"), " order by PBRQ ", list.toArray());
        return super.queryPage(sb, ResultHandlerConstant.JSONARRAY);
    }

    /**
     * 查询所有号源信息
     * @param map
     * @return
     */
    @Override
    public JSONArray queryAll(Map map) {
        JSONObject sqlObj = this.getQueryPageSql(map);
        List list = (List)sqlObj.get("sqlParams");

        SQLBean bean = new SQLBean(sqlObj.getString("sql")+" order by PBRQ ", list.toArray());
        return super.queryList(bean);
    }

    /**
     * 分页语句拼接
     * @param map
     * @return
     */
    private JSONObject getQueryPageSql(Map<String,String> map){
        JSONObject result = new JSONObject();

        StringBuilder sbud = new StringBuilder("select yljgmc ,ksmc_1 yjksmc, ksmc_2 ejksmc , ysxm , zylxmc , hylxmc , yysd , \n")
                .append("yysjd , pbrq , yyzhys , syhys , yyys , hyzt , hybh from V_ZYZX_HYXX  where 1 = 1 \n");
        List list = new ArrayList();
        String value = (String)map.get("YLJGDM");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and yljgdm = ? \n");
            list.add(value);
        }
        value = (String)map.get("YJKSDM");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and KSDM_1 = ? \n");
            list.add(value);
        }
        value = (String)map.get("EJKSDM");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and KSDM_2 = ? \n");
            list.add(value);
        }
        value = (String)map.get("YSGH");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and YSGH = ? \n");
            list.add(value);
        }
        value = (String)map.get("PBRQ_START");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and PBRQ >= TO_DATE(?,'yyyy-MM-dd') \n");
            list.add(value);
        }
        value = (String)map.get("PBRQ_END");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and PBRQ <= TO_DATE(?,'yyyy-MM-dd') \n");
            list.add(value);
        }
        value = (String)map.get("XZQHDM");
        if(!StringUtils.isEmpty(value)){
            sbud.append(" and XZQHDM LIKE ? \n");
            list.add(value + "%");
        }
        result.put("sql", sbud.toString());
        result.put("sqlParams", list);
        return result;
    }
}
